package com.hudilao.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hudilao.common.BaseContext;
import com.hudilao.common.R;
import com.hudilao.entity.Cart;
import com.hudilao.service.CartService;

import com.hudilao.service.DishService;
import com.hudilao.service.SetmealService;
import com.hudilao.vo.CartShowVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/user/cart")
public class CartController {
    @Autowired
    private CartService cartService;


    @Autowired
    private SetmealService setmealService;


    /**
     * 查看购物车
     * @return
     */
    @GetMapping("/list")
    public R<List<CartShowVO>> list(){
        log.info("查看购物车");

        //获取当前用户
        long customerId = 5L;//BaseContext.getCurrentId();

        //查询当前用户购物车
        LambdaQueryWrapper<Cart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Cart::getCustomerId,customerId);
        queryWrapper.orderByAsc(Cart::getCreateTime);

        //
        List<Cart> list = cartService.list(queryWrapper);

        List<CartShowVO> cartShowVOS = list.stream().map((item) -> {
            CartShowVO cartShowVO = CartShowVO.builder()
                    .name(item.getName())
                    .amount(item.getAmount())           //需修改，应为菜品单价
                    .image(item.getImage())
                    .number(item.getNumber())
                    .build();
            return cartShowVO;
        }).collect(Collectors.toList());

        return R.success(cartShowVOS);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/remove")
    public R<String> remove(){

        //获取当前用户
        long customerId = BaseContext.getCurrentId();
        return R.success("清空购物车成功");
    }
}
